/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Mar 19, 2012  3:31:07 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.synchro.staging
 * File Name : FiApinvoicerentOutbServices.java
 * TODO
 */
package jpa.service.synchro.staging;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import org.apache.log4j.Logger;
import promis.utility.ACTION_HANDLER;
import promis.utility.CommonService;
import jpa.entitys.synchro.common.CommonEntity;
import jpa.entitys.synchro.staging.FiApinvoicerentOutb;
import jpa.service.synchro.common.JPAService;

/**
 * @author chatree
 *
 */
public class FiApinvoicerentOutbServices extends JPAService {
	
	private static final Logger log = Logger.getLogger(FiApinvoicerentOutbServices.class);

	/**
	 * @param em
	 */
	public FiApinvoicerentOutbServices(EntityManager em) {
		super(em);
		// TODO Auto-generated constructor stub
	}
	

	public FiApinvoicerentOutb findById(String id) throws Exception {		
		
		return super.ConvertToYoureObject(super.find(id, "FiApinvoicerentOutb.findById"));
	}
	
	public FiApinvoicerentOutb Updated(FiApinvoicerentOutb value) throws Exception {
		if(value != null)
		{
			return (FiApinvoicerentOutb) super.updated(value);
		}else
			return null;
	}
	
	public List<FiApinvoicerentOutb> findByinstanceNo(String instanceNo) throws Exception {
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("instanceNo", instanceNo);
		
		return super.ConvertToYoureObject(super.find("FiApinvoicerentOutb.findByinstanceNo", mapWhereValue));		
	}
	
	public List<FiApinvoicerentOutb> findIsSync(String issync) throws Exception {
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("issync", issync);
		
		return super.ConvertToYoureObject(super.find("FiApinvoicerentOutb.findIsSync", mapWhereValue));		
	}
	
	public FiApinvoicerentOutb InsertedOrUpdated(FiApinvoicerentOutb vlaue) throws Exception {
		FiApinvoicerentOutb obj = vlaue;
		try{		
			if(CommonService.IsNull(obj.getPromisStagingId()))
			{
				String id = super.SequenceNext("FI_APINVOICERENT_OUTB");					
				obj.setPromisStagingId(id);
						
				obj.setSyncCreatedby(this.getUser());
				obj.setSyncCreatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				obj.setIsactive("Y");
				obj.setIssync("N");
				
				if(CommonService.IsNull(obj.getUpdatedOn()))
					obj.setActionHandler(ACTION_HANDLER.INSERTED.toString());
				else
					obj.setActionHandler(ACTION_HANDLER.UPDATED.toString());
				
				super.inserted(obj);
			}else{
				
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	public List<FiApinvoicerentOutb> Inserted(List<FiApinvoicerentOutb> vlauelist) throws Exception {
		log.info("Inserted ====> ");
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(FiApinvoicerentOutb obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getPromisStagingId()))
				{
					String id = super.SequenceNext("FI_APINVOICERENT_OUTB");					
					obj.setPromisStagingId(id);
							
					obj.setSyncCreatedby(this.getUser());
					obj.setSyncCreatedon(this.getCurrent_date());
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					obj.setIsactive("Y");
					obj.setIssync("N");
					
					if(CommonService.IsNull(obj.getUpdatedOn()))
						obj.setActionHandler(ACTION_HANDLER.INSERTED.toString());
					else
						obj.setActionHandler(ACTION_HANDLER.UPDATED.toString());
					
					log.info(obj.toString());
					common_lst.add(obj);
				}	
			}
						
			if(common_lst != null && common_lst.size() >= 1)
				super.inserted(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}
	
	public List<FiApinvoicerentOutb> Updated(List<FiApinvoicerentOutb> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(FiApinvoicerentOutb obj : vlauelist)
			{
				if(!CommonService.IsNull(obj.getPromisStagingId()))
				{
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					common_lst.add(obj);
				}
			}
			
			if(common_lst != null && common_lst.size() >= 1)
				super.updated(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}

}
